﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using IntelligentEscalator.BUS;
using IntelligentEscalator.Models;

namespace IntelligentEscalator.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class MaintainController : ControllerBase
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        private JwtSettings _jwtSettings;
        public MaintainController(IHostingEnvironment hostingEnvironment, IOptions<JwtSettings> jwtSettingsAccesser)
        {
            _hostingEnvironment = hostingEnvironment;
            _jwtSettings = jwtSettingsAccesser.Value;
        }
        /// <summary>
        /// 查询维保历史
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryMaintainList")]
        public ResponseMessage QueryMaintainList(MaintainInfo maintaininfo)
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.QueryMaintainHistoryList(maintaininfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询维保历史失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询维保历史";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 载入维保信息
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("LoadMaintain")]
        public ResponseMessage LoadMaintain(MaintainInfo maintaininfo)
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.LoadMaintain(maintaininfo.id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入维保信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入维保信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询维保计划信息
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryMaintainPlanInfo")]
        public ResponseMessage QueryMaintainPlanInfo(MaintainInfo maintaininfo)
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.QueryMaintainPlanInfo(maintaininfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询维保计划失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询维保计划";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除保养工单
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelMaintain")]
        public ResponseMessage DelMaintain(MaintainInfo maintaininfo)
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.DeleteMaintain(maintaininfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除保养工单" + maintaininfo.maintenanceNo;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除保养工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除保养工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 工单状态列表
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GetStateList")]
        public ResponseMessage GetStateList()
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.GetStateList();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "工单状态获取失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询工单状态";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 新增保养工单
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddMaintain")]
        public ResponseMessage AddMaintain(MaintainInfo maintaininfo)
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.AddMaintain(maintaininfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增保养工单" + maintaininfo.maintenanceNo;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "新增保养工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增保养工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 编辑保养工单
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateMaintain")]
        public ResponseMessage UpdateMaintain(MaintainInfo maintaininfo)
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.UpdateMaintain(maintaininfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "编辑保养工单" + maintaininfo.maintenanceNo;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "编辑保养工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "编辑保养工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询年检历史
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryAnnualMaintainList")]
        public ResponseMessage QueryAnnualMaintainList(MaintainInfo maintaininfo)
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.QueryAnnualMaintainHistoryList(maintaininfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询维保历史失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询维保历史";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportMaintain")]
        public ResponseMessage ExportMaintain(MaintainInfo maintainInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                MaintainBUS bus = new MaintainBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;

                message = bus.ExportMaintain(maintainInfo, sWebRootFolder);

            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出维保计划";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }


        /// <summary>
        /// 生成工单号
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GenerateMaintainNo")]
        public ResponseMessage GenerateInspectionNo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                MaintainBUS bus = new MaintainBUS();
                message = bus.GenerateMaintainNo();
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "生成维保工单号异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "生成维保工单号";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 导出保养历史
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportMaintainHistory")]
        public ResponseMessage ExportMaintainHistory(MaintainInfo maintainInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                MaintainBUS bus = new MaintainBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;

                message = bus.ExportMaintainHistory(maintainInfo, sWebRootFolder);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "导出保养历史";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出保养历史";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 导出年检历史
        /// </summary>
        /// <param name="maintaininfo">保养信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportAnnualMaintainHistory")]
        public ResponseMessage ExportAnnualMaintainHistory(MaintainInfo maintainInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                MaintainBUS bus = new MaintainBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;

                message = bus.ExportAnnualMaintainHistory(maintainInfo, sWebRootFolder);

                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "导出年检历史";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }

            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出年检历史";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除保养工单
        /// </summary>
        /// <param name="ids">ids</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BatchDelMaintain")]
        public ResponseMessage BatchDelMaintain(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            MaintainBUS BUS = new MaintainBUS();
            try
            {
                message = BUS.BatchDeleteMaintain(ids);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除保养工单";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "批量删除保养工单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除保养工单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
